package com.jinglvhuanjing.app.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.jinglvhuanjing.app.R;
import com.jinglvhuanjing.app.helper.DisplayHelper;


public class BaseDialog extends android.app.Dialog {

    private boolean cancelable = true;
    private boolean fullScreen = false;
    private boolean fullScreenWidth = false;
    private int width = 0;
    private int gravity = Gravity.CENTER;
    private View contentView;
    private boolean isAnimating = false;
    private boolean fullScreenHeight = false;
    private Animation showAnimation;
    private Animation dismissAnimation;

    public BaseDialog(@NonNull Context context) {
        this(context, R.style.dialog_style);
    }

    public BaseDialog(@NonNull Context context, int styleRes) {
        super(context, styleRes);
        this.setCancelable(true);
        this.setCanceledOnTouchOutside(true);
        this.setFullScreenWidth(false);
    }

    /**
     * 设置显示动画
     */
    public void setShowAnimation(@Nullable Animation showAnimation) {
        this.showAnimation = showAnimation;
    }

    public void setFullScreen(boolean fullScreen) {
        this.fullScreen = fullScreen;
    }

    /**
     * 设置隐藏动画
     */
    public void setDismissAnimation(@Nullable Animation dismissAnimation) {
        this.dismissAnimation = dismissAnimation;
    }

    /**
     * 是否铺满屏幕宽度
     */
    public void setFullScreenWidth(boolean mFullScreenWidth) {
        this.fullScreenWidth = mFullScreenWidth;
    }

    /**
     * 是否铺满屏幕高度
     */
    public void setFullScreenHeight(boolean mFullScreenHeight) {
        this.fullScreenHeight = mFullScreenHeight;
    }

    /**
     * 设置Dialog显示位置
     */
    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    /**
     * 设置是否可使用返回键关闭
     * @param cancelable 是否可关闭
     */
    @Override
    public void setCancelable(boolean cancelable) {
        super.setCancelable(cancelable);
        this.cancelable = cancelable;
    }

    /**
     * 设置是否可通过触摸其他未知关闭
     * @param cancel 是否可关闭
     */
    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
        if (cancel && !cancelable) {
            this.cancelable = true;
        }
    }

    public void setWidth(int width) {
        this.width = width;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Window window = getWindow();
        if (window == null) {
            return;
        }
        final int screenWidth = DisplayHelper.getScreenWidth(getContext());
        final int screenHeight = DisplayHelper.getScreenHeight(getContext());
        final int dialogWidth = Math.max(screenWidth, screenHeight);

        WindowManager.LayoutParams params = window.getAttributes();
        if (params == null) {
            params = new WindowManager.LayoutParams();
        }

        params.gravity = this.gravity;
        if (fullScreen) {
            window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        } else {
            if (fullScreenHeight) {
                params.height = ViewGroup.LayoutParams.MATCH_PARENT;
            } else {
                params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            }
            if (fullScreenWidth) {
                params.width = dialogWidth;
            } else {
                if (width != 0) {
                    params.width = width;
                } else {
                    int marginSide2 = DisplayHelper.dp2px(getContext(), 30);
                    params.width = dialogWidth - marginSide2;
                }
            }
        }
        window.setAttributes(params);
    }

    @Override
    public void addContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
        super.addContentView(view, params);
        this.contentView = view;
    }

    @Override
    public void setContentView(@NonNull View view) {
        super.setContentView(view);
        this.contentView = view;
    }

    @Override
    public void setContentView(int layoutRes) {
        this.contentView = LayoutInflater.from(getContext()).inflate(layoutRes, null);
        super.setContentView(contentView);
    }

    @Override
    public void setContentView(@NonNull View view, @Nullable ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        this.contentView = view;
    }

    /**
     * 是否有动画效果
     */
    public boolean isAnimation() {
        return (showAnimation != null && dismissAnimation != null);
    }

    @Override
    public void show() {
        super.show();
        if (this.isAnimation() && contentView != null) {
            showAnimation.setAnimationListener(showAnimationListener);
            contentView.startAnimation(showAnimation);
        }
    }

    @Override
    public void dismiss() {
        if (this.isAnimation()) {
            if (!isAnimating && contentView != null) {
                dismissAnimation.setAnimationListener(closeAnimationListener);
                contentView.startAnimation(dismissAnimation);
            }
        } else {
            super.dismiss();
        }
    }

    /**
     * 显示动画监听器
     */
    private final Animation.AnimationListener showAnimationListener = new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            isAnimating = true;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            isAnimating = false;
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };

    /**
     * 关闭动画监听器
     */
    private final Animation.AnimationListener closeAnimationListener = new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            isAnimating = true;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            isAnimating = false;
            if (contentView != null) {
                contentView.post(() -> {
                    try {
                        BaseDialog.super.dismiss();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };
}